﻿using System;
using System.Windows;
using KlavoNotifications.Fetch;
using KlavoNotifications.Notification;
using KlavoNotifications.Storage;
using KlavoNotifications.Utils;
using NLog;
using Ninject;

namespace KlavoNotifications
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        private static StandardKernel _kernel;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;


            _kernel = new StandardKernel();
            _kernel.Bind<Fetcher>().To<Fetcher>().InSingletonScope();
            _kernel.Bind<TaskBar>().To<TaskBar>().InSingletonScope();
            _kernel.Bind<Notifier>().To<Notifier>().InSingletonScope();
            _kernel.Bind<NotificationStorage>().To<NotificationStorage>().InSingletonScope();     
                  
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {            
            _logger.Error("CurrentDomain_UnhandledException", e.ExceptionObject.ToString());
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            _logger.ErrorException("App_DispatcherUnhandledException", e.Exception);
        }

        public static T Get<T>()
        {
            return _kernel.Get<T>();
        }
    }
}
